/*
 * $Id$
 *
 * Copyright (C) Paris8-IUT de Montreuil, 2012-2013
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */
package fr.paris8.iut.info.iddl.conf;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Vector;
import java.util.List;
import java.util.Iterator;
import java.util.Enumeration;
import java.util.Collections;

import fr.inrialpes.exmo.align.impl.rel.EquivRelation;
import fr.inrialpes.exmo.align.impl.rel.SubsumeRelation;
import fr.inrialpes.exmo.align.impl.rel.SubsumedRelation;

import org.semanticweb.owl.align.AlignmentVisitor;
import org.semanticweb.owl.align.Cell;
import org.semanticweb.owl.align.Alignment;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyID;
import org.semanticweb.owlapi.model.OWLOntologyChange;
import org.semanticweb.owlapi.model.AddAxiom;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLClassAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLDataPropertyExpression;
import org.semanticweb.owlapi.model.OWLProperty;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLSubClassOfAxiom;
import org.semanticweb.owlapi.model.OWLSubPropertyAxiom;
import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom;
import org.semanticweb.owlapi.model.OWLSubDataPropertyOfAxiom;
import org.semanticweb.owlapi.model.OWLIndividualAxiom;
import org.semanticweb.owlapi.model.OWLPropertyAxiom;
import org.semanticweb.owlapi.model.OWLObjectPropertyAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentObjectPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLEquivalentDataPropertiesAxiom;
import org.semanticweb.owlapi.model.OWLDataPropertyAxiom;
import org.semanticweb.owlapi.model.AxiomType;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.util.OWLOntologyChangeVisitorAdapter;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
import uk.ac.manchester.cs.owl.owlapi.OWLOntologyImpl;

import fr.inrialpes.exmo.align.impl.renderer.OWLAxiomsRendererVisitor;
import fr.inrialpes.exmo.ontowrap.LoadedOntology;
import fr.inrialpes.exmo.ontowrap.Ontology;
import fr.inrialpes.exmo.ontowrap.OntologyFactory;
import fr.inrialpes.exmo.align.parser.AlignmentParser;
import fr.inrialpes.exmo.align.impl.ObjectAlignment;
import fr.inrialpes.exmo.align.impl.BasicAlignment;
import fr.inrialpes.exmo.align.impl.URIAlignment;
import fr.paris8.iut.info.iddl.AlignServer;
import fr.paris8.iut.info.iddl.IDDLReasonerImpl;
import fr.paris8.iut.info.iddl.IDDLReasoner;
import java.net.URL;

public class SystemData {
	
	//keys of maps are uris of local concepts and values are uris of global concepts 
	//So each local concept uri has a unique global concept uri
	private HashMap<String, String> globalConceptMap = null;
	private HashMap<String, String> globalRoleMap = null;
	private HashMap<String, String> globalObjectRoleMap = null;
	private HashMap<String, String> globalDataRoleMap = null;
	private HashMap<String, String> globalIndMap = null;
	private Set<String> localUris = null;

	private Set<String> alignLocations = null;
	private LocalEntityMap   localMap = null;
	
	//"Alignment Ontology" includes axioms corresponding to all correspondences in alignments 
	private  OWLOntology 	initialAlignOnto = null;
	private  OWLOntology 	alignOnto = null;
	
	//rethink !!! that may be overflow
	int fileId = 0;
    
	public SystemData( Vector<URI>   alignUris ) {
		int fileId=0;

		if (alignUris.size() == 0 || alignUris == null) return;
		
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager( );
		
		globalConceptMap 	= new HashMap();
		globalRoleMap    	= new HashMap();
		globalDataRoleMap  	= new HashMap();
		globalObjectRoleMap 	= new HashMap();
		localMap         	= new LocalEntityMap();
		localUris        	= new HashSet();
		alignLocations        	= new HashSet();
		globalIndMap     	= new HashMap();
		
		//OWLOntologyManager manager = OWLManager.createOWLOntologyManager( );
		//OWLOntology initialAlignOnto = manager.loadOntology( URI.create( "http://alignapi.gforge.inria.fr/tutorial/myOnto.owl" ) );
		OWLDataFactoryImpl facto = new OWLDataFactoryImpl(); 
		//OWLOntologyChangeVisitorAdapter xc = new OWLOntologyChangeVisitorAdapter();
		
		String aoUriStr = IDDLReasoner.uriRoot + IDDLReasonerImpl.getGlobalOntoName() + ".owl";
		IRI aoUri = IRI.create(  aoUriStr );
		
		String aUriStr = IDDLReasoner.uriRoot + IDDLReasonerImpl.getGlobalOntoName() +".owl";
		IRI aUri = IRI.create(  aUriStr );
		
		try {
			//initialAlignOnto = new OWLOntology( manager, new OWLOntologyID( aoUri ) );
			initialAlignOnto = manager.createOntology(  aoUri ) ;
			//alignOnto = new OWLOntology( manager, new OWLOntologyID( aUri ) );
			alignOnto = manager.createOntology(  aUri ) ;		
		}
		catch (OWLOntologyCreationException ex) { ex.printStackTrace(); }

		globalConceptMap.put( aoUriStr + "#Thing", aoUriStr + "#Thing" );
		OWLClass cls = facto.getOWLClass( IRI.create( aoUriStr + "#Thing" ) );
		OWLClassAxiom thAx = facto.getOWLEquivalentClassesAxiom( cls, facto.getOWLThing());
	        
 		AddAxiom thAddaxiom = new AddAxiom( initialAlignOnto, thAx);
 		//thAddaxiom.accept( xc );
		manager.applyChange( thAddaxiom );

		Hashtable<String,Alignment>  alignTable = new Hashtable<String,Alignment>();
		String defaultHost = "aserv.inrialpes.fr";
		String defaultPort = "80";
		AlignServer onlineAlign = new AlignServer(defaultPort, defaultHost);
		try {
			AlignmentParser ap = new AlignmentParser( 0 );
		        ap.setEmbedded(true);
		    
			for(int i=0; i< alignUris.size(); i++) {
				
				String owlStr =  onlineAlign.getOWLAlignment(alignUris.get( i ).toString());
				String rdfStr =  onlineAlign.getRDFAlignment(alignUris.get( i ).toString());
				Alignment align = (Alignment)ap.parseString( rdfStr );
				alignTable.put( alignUris.get(i).toString(), align );
				String uri1 = align.getOntology1URI().toString();
				String uri2 = align.getOntology2URI().toString();

				localUris.add(uri1);
				localUris.add(uri2);
				//System.out.println( "File 1="+ align.getFile1().toString()  );
				//System.out.println( "File 2="+ align.getFile2().toString()  );
				alignLocations.add(align.getFile1().toString());
				alignLocations.add(align.getFile2().toString());
				 

				String fName =  Integer.toString(fileId++) + ".owl" ;
				File owlFile = new File (fName);
				
				//if (owlFile.exists()) owlFile.delete();
			 
				FileWriter out = new FileWriter( owlFile );
			
				out.write( owlStr );
				out.flush();
				out.close();

			   
		        localMap.addConcept(uri1, uri1 + "#Thing");
		        localMap.addConcept(uri2, uri2 + "#Thing");
		        OWLOntology onto = manager.loadOntologyFromOntologyDocument( IRI.create("file:"+ owlFile.getAbsolutePath()) );	     
			//JE2010: Set<OWLClassAxiom> cAxSet = onto.getClassAxioms();
			// I am not really sure that this is the same
			Set<OWLClassAxiom> cAxSet = onto.getGeneralClassAxioms();
		         
			    for(OWLClassAxiom ax : cAxSet) {
			    	
			    	if(ax instanceof OWLSubClassOfAxiom) {
			    		 
			    		OWLSubClassOfAxiom axs = (OWLSubClassOfAxiom)ax;
			    		//if( !axs.getSubClass().isAnonymous() &&  !axs.getSuperClass().isAnonymous() ){
			    		//	System.out.println( "class axiom not anonym " );
			    			OWLClass c1 = (OWLClass) axs.getSubClass();
			    			OWLClass c2 = (OWLClass) axs.getSuperClass();
			    			String uriStr1 = c1.getIRI().toString();
			    			String uriStr2 = c2.getIRI().toString();
			    			
			    			String name1 = Integer.toString( IDDLReasonerImpl.getGlobalEntityId() );
					        String name2 = Integer.toString( IDDLReasonerImpl.getGlobalEntityId() );
					        
					        localMap.addConcept( uri1, uriStr1 );
					        globalConceptMap.put(uriStr1, aoUriStr + "#" + name1 );
					         
					        localMap.addConcept(uri2, uriStr2 );
					        globalConceptMap.put(uriStr2, aoUriStr + "#" + name2 );
					         
					        //build Alignment Ontology
					        OWLClass cls1 = facto.getOWLClass( IRI.create( aoUriStr + "#" + name1 ) );
					        OWLClass cls2 = facto.getOWLClass( IRI.create( aoUriStr + "#" + name2 ) );
					        Set<OWLClassExpression> clsSet = new HashSet<OWLClassExpression>(); 
					        clsSet.add(cls1);
					        clsSet.add(cls2);
					         
					        OWLClassAxiom clsAx = facto.getOWLSubClassOfAxiom( cls1, cls2 );
					          
					          
					        AddAxiom addaxiom = new AddAxiom(initialAlignOnto, clsAx);
						//	addaxiom.accept( xc );
						manager.applyChange( addaxiom );
			    		//}
			    	}
			    	
			    	if(ax instanceof OWLEquivalentClassesAxiom) {
			    		 
			    		OWLEquivalentClassesAxiom axs = (OWLEquivalentClassesAxiom)ax;
			    		Set<OWLClassExpression> opCls = axs.getClassExpressions();
			    		//supposed that there are two props in the order
			    		int counter = 0;
			    		String[] names = new String[2];
			    		for(OWLClassExpression op : opCls) {
			    			
			    			//if(!op.isAnonymous()) {
			    					OWLClass r1 = op.asOWLClass();
			    					String uriStr1 = r1.getIRI().toString();
			    					names[counter] = Integer.toString( IDDLReasonerImpl.getGlobalEntityId() );
							        if(counter == 0) { 
							        	localMap.addConcept(uri1, uriStr1 );
							        	 
							        } else if(counter==1) {
							        	localMap.addConcept(uri2, uriStr1 );
							        }
							        globalConceptMap.put(uriStr1, aoUriStr + "#" + names[counter] );
			    					
			    			//}
			    			counter++;
			    			if(counter > 1 ) break;
			    		}
			    		
			    		OWLClass cls1 = facto.getOWLClass( IRI.create( aoUriStr + "#" + names[0] ) );
				        OWLClass cls2 = facto.getOWLClass( IRI.create( aoUriStr + "#" + names[1] ) );
				        Set<OWLClassExpression> clsSet = new HashSet<OWLClassExpression>(); 
				        clsSet.add(cls1);
				        clsSet.add(cls2);
				         
				        OWLClassAxiom clsAx = facto.getOWLEquivalentClassesAxiom( cls1, cls2 );
				        
			    		AddAxiom addaxiom = new AddAxiom(initialAlignOnto, clsAx);
						//addaxiom.accept(xc);
					manager.applyChange( addaxiom );
			    		 
			    	}
			    }
			    	
			    //Set<OWLPropertyAxiom> rAxSet = onto.getObjectPropertyAxioms();
			    Set<OWLAxiom> rAxSet = onto.getAxioms();
			    
			    for(OWLAxiom ax : rAxSet) {	
			    	 
			    	//if(ax instanceof OWLSubPropertyAxiom) {
				if( ax.isOfType( AxiomType.SUB_OBJECT_PROPERTY ) ) {
			    		OWLSubPropertyAxiom axs = (OWLSubPropertyAxiom)ax;
			    		 
			    		//if( !axs.getSubProperty().isAnonymous() &&  !axs.getSuperProperty().isAnonymous() ){
			    			 
			    			OWLProperty r1 = (OWLProperty) axs.getSubProperty();
			    			OWLProperty r2 = (OWLProperty) axs.getSuperProperty();
			    			String uriStr1 = r1.getIRI().toString();
			    			String uriStr2 = r2.getIRI().toString();
			    			String name1 = Integer.toString( IDDLReasonerImpl.getGlobalEntityId() );
					        String name2 = Integer.toString( IDDLReasonerImpl.getGlobalEntityId() );
					        
					        
					        localMap.addRole(uri1, uriStr1 );
				        	globalRoleMap.put(uriStr1, aoUriStr + "#" + name1 );
				        	 
				        	localMap.addRole(uri2, uriStr2 );
				        	globalRoleMap.put(uriStr2, aoUriStr + "#" + name2 );
				        	 
				        	//build Alignment Ontology
				        	if(ax instanceof OWLSubObjectPropertyOfAxiom) {
				        		OWLProperty prop1 = facto.getOWLObjectProperty( IRI.create( aoUri + "#" + name1 ) );
				        		OWLProperty prop2 = facto.getOWLObjectProperty( IRI.create( aoUri + "#" + name2 ) );
					         
				        		//Set<OWLObjectPropertyExpression> propSet = new HashSet<OWLObjectPropertyExpression>(); 
				        		//propSet.add( (OWLObjectPropertyExpression) prop1);
				        		//propSet.add( (OWLObjectPropertyExpression) prop2);
					         
				        		OWLPropertyAxiom propAx = facto.getOWLSubObjectPropertyOfAxiom(  (OWLObjectPropertyExpression)prop2, (OWLObjectPropertyExpression)prop1 );
				        		AddAxiom addaxiom = new AddAxiom(initialAlignOnto, propAx);
				        		//addaxiom.accept(xc);
				        		manager.applyChange( addaxiom );
				        	}
				        	
				        	if(ax instanceof OWLSubDataPropertyOfAxiom) {
				        		//OWLProperty prop1 = facto.getOWLDataProperty( IRI.create( aoUri + "#" + name1 ) );
				        		//OWLProperty prop2 = facto.getOWLDataProperty( IRI.create( aoUri + "#" + name2 ) );
					         
				        		OWLProperty prop1 = facto.getOWLObjectProperty( IRI.create( aoUri + "#" + name1 ) );
				        		OWLProperty prop2 = facto.getOWLObjectProperty( IRI.create( aoUri + "#" + name2 ) );
					         
				        		//OWLPropertyAxiom propAx = facto.getOWLSubDataPropertyOfAxiom(  (OWLDataPropertyExpression)prop2, (OWLDataPropertyExpression)prop1 );
				        		OWLPropertyAxiom propAx = facto.getOWLSubObjectPropertyOfAxiom(  (OWLObjectPropertyExpression)prop2, (OWLObjectPropertyExpression)prop1 );
				        		AddAxiom addaxiom = new AddAxiom(initialAlignOnto, propAx);
				        		//addaxiom.accept(xc);
				        		manager.applyChange( addaxiom );
				        	}
					        
			    		//}
			    	}
			    	//if(ax instanceof OWLEquivalentObjectPropertiesAxiom) {
			    	if(ax.isOfType( AxiomType.EQUIVALENT_OBJECT_PROPERTIES ) ) {
			    		OWLEquivalentObjectPropertiesAxiom axs = (OWLEquivalentObjectPropertiesAxiom)ax;
			    		 
			    		Set<OWLObjectPropertyExpression> opProp = axs.getProperties();
			    		//supposed that there are two props in the order
			    		int counter = 0;
			    		String[] names = new String[2];
			    		for(OWLObjectPropertyExpression op : opProp) {
			    			
			    			//if(!op.isAnonymous()) {
			    				
			    					OWLObjectProperty r1 = op.asOWLObjectProperty();
			    					String uriStr1 = r1.getIRI().toString();
			    					names[counter] = Integer.toString( IDDLReasonerImpl.getGlobalEntityId() );
							        if(counter == 0) { 
							        	localMap.addRole(uri1, uriStr1 );
							        	 
							        } else if(counter==1) {
							        	localMap.addRole(uri2, uriStr1 );
							        }
							        globalRoleMap.put(uriStr1, aoUriStr + "#" + names[counter] );
			    					
			    			//}
			    			counter++;
			    			if(counter > 1) break;
			    		}
			    		
			    		//build Alignment Ontology
			        	OWLProperty prop1 = facto.getOWLObjectProperty( IRI.create( aoUri + "#" + names[0] ) );
				        OWLProperty prop2 = facto.getOWLObjectProperty( IRI.create( aoUri + "#" + names[1] ) );
				         
				        Set<OWLObjectPropertyExpression> propSet = new HashSet<OWLObjectPropertyExpression>(); 
				        propSet.add( (OWLObjectPropertyExpression) prop1);
				        propSet.add( (OWLObjectPropertyExpression) prop2);
				        Set<OWLObjectPropertyExpression> sp = new HashSet();
			
				        OWLEquivalentObjectPropertiesAxiom propAx = facto.getOWLEquivalentObjectPropertiesAxiom( propSet );
				        AddAxiom addaxiom = new AddAxiom(initialAlignOnto, propAx);
						//addaxiom.accept(xc);
						manager.applyChange( addaxiom );
			    	}
			    }
			    
			    //Set<OWLPropertyAxiom> drAxSet = onto.getDataPropertyAxioms();
 			    Set<OWLAxiom> drAxSet = onto.getAxioms();
			    for(OWLAxiom ax : drAxSet) {	
			    	 
			    	if(ax.isOfType( AxiomType.EQUIVALENT_DATA_PROPERTIES )) {
			    	//if(ax instanceof OWLEquivalentDataPropertiesAxiom) {
			    		
			    		OWLEquivalentDataPropertiesAxiom axs = (OWLEquivalentDataPropertiesAxiom)ax;
			    		Set<OWLDataPropertyExpression> opProp = axs.getProperties();
			    		//supposed that there are two props in the order
			    		int counter = 0;
			    		String[] names = new String[2];
			    		for(OWLDataPropertyExpression op : opProp) {
			    			
			    			//if( !op.isAnonymous()) {
			    					OWLDataProperty r1 = op.asOWLDataProperty();
			    					 
			    					String uriStr1 = r1.getIRI().toString();
			    					names[counter] = Integer.toString( IDDLReasonerImpl.getGlobalEntityId() );
							        if(counter == 0) { 
							        	localMap.addRole(uri1, uriStr1 );
							        	 
							        } else if(counter==1) {
							        	localMap.addRole(uri2, uriStr1 );
							        }
							  
							        globalRoleMap.put(uriStr1, aoUriStr + "#" + names[counter] );
			    					
			    			//}
			    			counter++;
			    			if(counter > 1) break;
			    		}
			    		
			    		//build Alignment Ontology
			        	//OWLProperty prop1 = facto.getOWLDataProperty( IRI.create( aoUri + "#" + names[0] ) );
				        //OWLProperty prop2 = facto.getOWLDataProperty( IRI.create( aoUri + "#" + names[1] ) );
			    		OWLProperty prop1 = facto.getOWLObjectProperty( IRI.create( aoUri + "#" + names[0] ) );
				        OWLProperty prop2 = facto.getOWLObjectProperty( IRI.create( aoUri + "#" + names[1] ) );
				        
				        //Set<OWLDataPropertyExpression> propSet = new HashSet<OWLDataPropertyExpression>(); 
				        //propSet.add( (OWLDataPropertyExpression) prop1);
				        //propSet.add( (OWLDataPropertyExpression) prop2);
				        //Set<OWLDataPropertyExpression> sp = new HashSet();
			
				        //OWLEquivalentDataPropertiesAxiom propAx = facto.getOWLEquivalentDataPropertiesAxiom( propSet );
				        
				        Set<OWLObjectPropertyExpression> propSet = new HashSet<OWLObjectPropertyExpression>(); 
				        propSet.add( (OWLObjectPropertyExpression) prop1);
				        propSet.add( (OWLObjectPropertyExpression) prop2);
				        //Set<OWLObjectPropertyExpression> sp = new HashSet();
			
				        OWLEquivalentObjectPropertiesAxiom propAx = facto.getOWLEquivalentObjectPropertiesAxiom( propSet );
				        AddAxiom addaxiom = new AddAxiom(initialAlignOnto, propAx);
						//addaxiom.accept(xc);
						manager.applyChange( addaxiom );
			    	}			
			    }   
			}		
		}
		catch ( Exception ex ) { ex.printStackTrace(); };
		
	}
	
	public OWLOntology OWLAlign2OWLOnto( Alignment  align ) {
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager( );
		OWLOntologyChangeVisitorAdapter xc = new OWLOntologyChangeVisitorAdapter();
		OWLDataFactoryImpl facto = new OWLDataFactoryImpl();
		
		String aUriStr = IDDLReasoner.uriRoot + IDDLReasonerImpl.getGlobalOntoName() +".owl";
		IRI aUri = IRI.create(  aUriStr );
		
		//OWLOntology OWLAlign = new OWLOntology( manager, new OWLOntologyID( aUri ) );
		OWLOntology OWLAlign = null;
		try {
	 		OWLAlign = manager.createOntology( aUri  );
		}
		catch (OWLOntologyCreationException ex) { ex.printStackTrace(); }

		BasicAlignment al = (BasicAlignment)align;
		String uri1 = al.getOntology1URI().toString();
		String uri2 = al.getOntology2URI().toString();
		 
		String location1 = al.getFile1().toString();
		String location2 = al.getFile2().toString();
		try {		
		
			OntologyFactory ontoFact = OntologyFactory.getFactory();
			LoadedOntology onto1 =  ontoFact.loadOntology(al.getOntologyObject1().getFile());
			LoadedOntology onto2 =  ontoFact.loadOntology(al.getOntologyObject2().getFile());				 
				
			for (Enumeration<Cell> cells = al.getElements() ; cells.hasMoreElements() ;) {

				
			         Cell cell = cells.nextElement();
			        
			         URI ur1 = cell.getObject1AsURI();
			          
			         Object ob1 = onto1.getEntity(ur1); 
			         //String obUri1 = onto1.getEntityURI( ob1 ).toString();
			         URI ur2 = cell.getObject2AsURI();
			         
			         Object ob2 = onto2.getEntity(ur2);
			          
			         //The classes treated here include nominal concepts
			         if ( onto1.isClass(ob1) && onto2.isClass(ob2) ) {
				         //build Alignment Ontology
				     OWLClass cls1 = facto.getOWLClass( IRI.create( ur1 ) );
				     OWLClass cls2 = facto.getOWLClass( IRI.create( ur2 ) );
				         Set<OWLClassExpression> clsSet = new HashSet<OWLClassExpression>(); 
				         clsSet.add(cls1);
				         clsSet.add(cls2);
				         OWLClassAxiom clsAx = null;
				         
				         if (cell.getRelation() instanceof EquivRelation) {
				        	 clsAx = facto.getOWLEquivalentClassesAxiom( clsSet );
				        	  
				        	  
				         } else if(cell.getRelation() instanceof SubsumedRelation ) {
				        	 clsAx = facto.getOWLSubClassOfAxiom( cls1, cls2 );
				        	  
				         } else {
				        	 clsAx = facto.getOWLSubClassOfAxiom( cls2, cls1 );
				        	  
				         }
				         AddAxiom addaxiom = new AddAxiom(OWLAlign, clsAx);
						 //addaxiom.accept(xc);
					 manager.applyChange( addaxiom );
						 
			         }
			         
			         if ( onto1.isObjectProperty(ob1) && onto2.isObjectProperty(ob2)) {
			        	  
			        	 
				     OWLProperty prop1 = facto.getOWLObjectProperty( IRI.create( ur1 ) );
				     OWLProperty prop2 = facto.getOWLObjectProperty( IRI.create( ur2 ) ); 
				         Set<OWLObjectPropertyExpression> propSet = new HashSet<OWLObjectPropertyExpression>(); 
				         propSet.add( (OWLObjectPropertyExpression) prop1);
				         propSet.add( (OWLObjectPropertyExpression) prop2);
				         OWLPropertyAxiom propAx = null;
				         
				         if (cell.getRelation() instanceof EquivRelation) {
				        	 propAx = facto.getOWLEquivalentObjectPropertiesAxiom( propSet );
				        	  
				        	 //System.out.println( "equi p1 = " +  obUri1);
					         //System.out.println( "equi p2 = " +  obUri2);
				        	  
				         } else if(cell.getRelation() instanceof SubsumedRelation ) {
				        	 propAx = facto.getOWLSubObjectPropertyOfAxiom(  (OWLObjectPropertyExpression)prop1, (OWLObjectPropertyExpression)prop2 );
				        	  
				        	 //System.out.println( "sub p1 = " +  obUri1);
					         //System.out.println( "sub p2 = " +  obUri2);
				         } else {
				        	  
				        	 propAx = facto.getOWLSubObjectPropertyOfAxiom(  (OWLObjectPropertyExpression)prop2, (OWLObjectPropertyExpression)prop1 );
				        	  
	
				         }
				         AddAxiom addaxiom = new AddAxiom(OWLAlign, propAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						 
			         }
			         
			         if ( onto1.isDataProperty(ob1) && onto2.isDataProperty(ob2)) {
			        	  
				     OWLProperty prop1 = facto.getOWLDataProperty( IRI.create( ur1 ) );
				     OWLProperty prop2 = facto.getOWLDataProperty( IRI.create( ur2 ) ); 
				         Set<OWLDataPropertyExpression> propSet = new HashSet<OWLDataPropertyExpression>(); 
				         propSet.add( (OWLDataPropertyExpression) prop1);
				         propSet.add( (OWLDataPropertyExpression) prop2);
				         OWLPropertyAxiom propAx = null;
				         
				          
				         if (cell.getRelation() instanceof EquivRelation) {
				        	 propAx = facto.getOWLEquivalentDataPropertiesAxiom( propSet );
				        	  
				        	 //System.out.println( "equi p1 = " +  obUri1);
					         //System.out.println( "equi p2 = " +  obUri2);
				        	  
				         } else if(cell.getRelation() instanceof SubsumedRelation ) {
				        	 propAx = facto.getOWLSubDataPropertyOfAxiom(  (OWLDataPropertyExpression)prop1, (OWLDataPropertyExpression)prop2 );
				        	  
				        	 //System.out.println( "sub p1 = " +  obUri1);
					         //System.out.println( "sub p2 = " +  obUri2);
				         } else {
				        	  
				        	 propAx = facto.getOWLSubDataPropertyOfAxiom(  (OWLDataPropertyExpression)prop2, (OWLDataPropertyExpression)prop1 );
				        	  
	
				         }
				         AddAxiom addaxiom = new AddAxiom(OWLAlign, propAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						 
						  
			        	  
			         }
				             
			         //there may be two kinds of individual correspondences : membership  
			          
			         if ( (onto1.isIndividual(ob1) && onto2.isClass(ob2))  ) {
			        	  
			          
				     OWLIndividual ind1 = facto.getOWLNamedIndividual( IRI.create( ur1 ) );
				     OWLClass cls1 = facto.getOWLClass( IRI.create( ur2 ) );
				         OWLIndividualAxiom indAx = null;
				         
				          
				         if (cell.getRelation() instanceof SubsumedRelation) {
				        	 
				        	 //System.out.println( "subsume d1 = " +  obUri1);
					         //System.out.println( "subsume c2 = " +  obUri2);
					         
					     indAx = facto.getOWLClassAssertionAxiom(cls1,ind1 );
				        	 
				         }
				         AddAxiom addaxiom = new AddAxiom(OWLAlign, indAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						 
						  
						  
			         }
			         
			         if ( (onto1.isClass(ob1) && onto2.isIndividual(ob2))  ) {
			        	  
			         
				     OWLIndividual ind1 = facto.getOWLNamedIndividual( IRI.create( ur1 ) );
				     OWLClass cls1 = facto.getOWLClass( IRI.create( ur2 ) );
				         OWLIndividualAxiom indAx = null;
				         
				          
				         if (cell.getRelation() instanceof SubsumeRelation) {
				        	 //indAx = facto.getOWLSameIndividualsAxiom( indSet );
					     indAx = facto.getOWLClassAssertionAxiom(cls1,ind1 );
				        	 
				        	 
				        	 //System.out.println( "subsumed c1 = " +  obUri1);
					         //System.out.println( "subsumed d2 = " +  obUri2);
				         }
				         
				         //System.out.println(" membership assertion added 2");
				         
				         AddAxiom addaxiom = new AddAxiom(OWLAlign, indAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						 
						  
			         }
		}
		} catch (Exception ex) { ex.printStackTrace(); } 

		return OWLAlign;
	}
		
	public SystemData( Hashtable<String,Alignment>  alignTables ) {
		
		if (alignTables.size() == 0 || alignTables == null)  return;
		
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager( );
		OWLOntologyChangeVisitorAdapter xc = new OWLOntologyChangeVisitorAdapter();
		
		globalConceptMap 	= new HashMap();
		globalRoleMap    	= new HashMap();
		globalObjectRoleMap 	= new HashMap();
		globalDataRoleMap   	= new HashMap();
		localMap         	= new LocalEntityMap();
		localUris	     	= new HashSet();
		alignLocations        	= new HashSet();
		globalIndMap     	= new HashMap();
		
		//OWLOntologyManager manager = OWLManager.createOWLOntologyManager( );
		//OWLOntology initialAlignOnto = manager.loadOntology( URI.create( "http://alignapi.gforge.inria.fr/tutorial/myOnto.owl" ) );
		OWLDataFactoryImpl facto = new OWLDataFactoryImpl(); 
		
		String aoUriStr = IDDLReasoner.uriRoot + IDDLReasonerImpl.getGlobalOntoName() +".owl";
		IRI aoUri = IRI.create(  aoUriStr );
		
		String aUriStr =  IDDLReasoner.uriRoot + IDDLReasonerImpl.getGlobalOntoName() +".owl";
		IRI aUri = IRI.create(  aUriStr );

		try {
			//initialAlignOnto = new OWLOntology( manager, new OWLOntologyID( aoUri ) );
			initialAlignOnto = manager.createOntology (aoUri);
			//alignOnto = new OWLOntology( manager, new OWLOntologyID( aUri ) );
			alignOnto =  manager.createOntology (aUri);
	 	}
		catch (OWLOntologyCreationException ex) { ex.printStackTrace(); }

		globalConceptMap.put(aoUriStr + "#Thing", aoUriStr + "#Thing" );
		
		 
		OWLClass cls = facto.getOWLClass( IRI.create( aoUriStr + "#Thing" ) );
		 

		OWLClassAxiom thAx = facto.getOWLEquivalentClassesAxiom( cls, facto.getOWLThing());
 		AddAxiom thAddaxiom = new AddAxiom(initialAlignOnto, thAx);
 		//thAddaxiom.accept( xc );
		manager.applyChange( thAddaxiom );
		
		cls = facto.getOWLClass( IRI.create( aUriStr + "#Thing" ) );
		thAx = facto.getOWLEquivalentClassesAxiom( cls, facto.getOWLThing());
 		thAddaxiom = new AddAxiom(alignOnto, thAx);
 		//thAddaxiom.accept( xc );
		manager.applyChange( thAddaxiom );
		
		
		Set<String> iterTable = alignTables.keySet();
		
		for(String iter : iterTable) {
			try {
				 
				BasicAlignment a = (BasicAlignment)alignTables.get(iter);

				String uri1 = a.getOntology1URI().toString();
				String uri2 = a.getOntology2URI().toString();
				localUris.add(uri1);
				localUris.add(uri2); 
				//System.out.println( "File 1="+ a.getFile1().toString()  );
				//System.out.println( "File 2="+ a.getFile2().toString()  );
				alignLocations.add(a.getFile1().toString());
				alignLocations.add(a.getFile2().toString());

			        //build Vocabulary
				
				OntologyFactory ontoFact = OntologyFactory.getFactory();
			        LoadedOntology onto1 =  ontoFact.loadOntology(a.getOntologyObject1().getFile());
				LoadedOntology onto2 =  ontoFact.loadOntology(a.getOntologyObject2().getFile());
				 
				
				for (Enumeration<Cell> cells = a.getElements() ; cells.hasMoreElements() ;) {

				
			         Cell cell = cells.nextElement();
			        
			         URI ur1 = cell.getObject1AsURI();
			         String obUri1 = ur1.toString();
			         Object ob1 = onto1.getEntity(ur1); 
			         //String obUri1 = onto1.getEntityURI( ob1 ).toString();
			         URI ur2 = cell.getObject2AsURI();
			         String obUri2 = ur2.toString();
			         Object ob2 = onto2.getEntity(ur2);
			        		         
			         String name1 = Integer.toString( IDDLReasonerImpl.getGlobalEntityId() );
			         String name2 = Integer.toString( IDDLReasonerImpl.getGlobalEntityId() );
			         
			         localMap.addConcept(uri1, uri1 + "#Thing");
			         localMap.addConcept(uri2, uri2 + "#Thing");
			         
			         //The classes treated here include nominal concepts
			         if ( onto1.isClass(ob1) && onto2.isClass(ob2) ) {
				         localMap.addConcept( uri1, obUri1 );
				         globalConceptMap.put(obUri1, aoUriStr + "#" + name1 );
				         
				         localMap.addConcept(uri2, obUri2 );
				         globalConceptMap.put(obUri2, aoUriStr + "#" + name2 );
				           
				         //build Alignment Ontology
				         OWLClass cls1 = facto.getOWLClass( IRI.create( aoUriStr + "#" + name1 ) );
				         OWLClass cls2 = facto.getOWLClass( IRI.create( aoUriStr + "#" + name2 ) );
				         Set<OWLClassExpression> clsSet = new HashSet<OWLClassExpression>(); 
				         clsSet.add(cls1);
				         clsSet.add(cls2);
				         OWLClassAxiom clsAx = null;
				         
				         OWLClass acls1 = facto.getOWLClass( IRI.create( obUri1 ) );
				         OWLClass acls2 = facto.getOWLClass( IRI.create( obUri2 ) );
				         Set<OWLClassExpression> aclsSet = new HashSet<OWLClassExpression>(); 
				         aclsSet.add(acls1);
				         aclsSet.add(acls2);
				         OWLClassAxiom aclsAx = null;
				         
				         
				         
				         if (cell.getRelation() instanceof EquivRelation) {
				        	 clsAx = facto.getOWLEquivalentClassesAxiom( clsSet );
				        	 aclsAx = facto.getOWLEquivalentClassesAxiom( aclsSet );
				        	 //System.out.println( "equi c1 = " +  obUri1);
					         //System.out.println( "equi c2 = " +  obUri2);
				        	  
				         } else if(cell.getRelation() instanceof SubsumedRelation ) {
				        	 clsAx = facto.getOWLSubClassOfAxiom( cls1, cls2 );
				        	 aclsAx = facto.getOWLSubClassOfAxiom( acls1, acls2 );
				         } else {
				        	 clsAx = facto.getOWLSubClassOfAxiom( cls2, cls1 );
				        	 aclsAx = facto.getOWLSubClassOfAxiom( acls2, acls1 );
				         }
				         AddAxiom addaxiom = new AddAxiom(initialAlignOnto, clsAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						 
						 addaxiom = new AddAxiom(alignOnto, aclsAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
			         }
			         
			         if ( onto1.isObjectProperty(ob1) && onto2.isObjectProperty(ob2)) {
			        	 localMap.addRole(uri1, obUri1 );
			        	 localMap.addObjectRole(uri1, obUri1 );
			        	 globalRoleMap.put(obUri1, aoUriStr + "#" + name1 );
			        	 globalObjectRoleMap.put(obUri1, aoUriStr + "#" + name1 );
			        	 
			        	 localMap.addRole(uri2, obUri2 );
			        	 localMap.addObjectRole(uri2, obUri2 );
			        	 globalRoleMap.put(obUri2, aoUriStr + "#" + name2 );
			        	 globalObjectRoleMap.put(obUri2, aoUriStr + "#" + name2 );


			        	 OWLProperty prop1 = facto.getOWLObjectProperty( IRI.create(new URL(aoUri + "#" + name1 )) );
				         OWLProperty prop2 = facto.getOWLObjectProperty( IRI.create(new URL(aoUri + "#" + name2 ) ));
				         Set<OWLObjectPropertyExpression> propSet = new HashSet<OWLObjectPropertyExpression>(); 
				         propSet.add( (OWLObjectPropertyExpression) prop1);
				         propSet.add( (OWLObjectPropertyExpression) prop2);
				         OWLPropertyAxiom propAx = null;
				         
				         OWLProperty aprop1 = facto.getOWLObjectProperty( IRI.create( obUri1 ) );
				         OWLProperty aprop2 = facto.getOWLObjectProperty( IRI.create( obUri2 ) ); 
				         Set<OWLObjectPropertyExpression> apropSet = new HashSet<OWLObjectPropertyExpression>(); 
				         apropSet.add( (OWLObjectPropertyExpression) aprop1);
				         apropSet.add( (OWLObjectPropertyExpression) aprop2);
				         OWLPropertyAxiom apropAx = null;
				         if (cell.getRelation() instanceof EquivRelation) {
				        	 propAx = facto.getOWLEquivalentObjectPropertiesAxiom( propSet );
				        	 apropAx = facto.getOWLEquivalentObjectPropertiesAxiom( apropSet );
				        	 //System.out.println( "equi p1 = " +  obUri1);
					         //System.out.println( "equi p2 = " +  obUri2);
				        	  
				         } else if(cell.getRelation() instanceof SubsumedRelation ) {
				        	 propAx = facto.getOWLSubObjectPropertyOfAxiom(  (OWLObjectPropertyExpression)prop1, (OWLObjectPropertyExpression)prop2 );
				        	 apropAx = facto.getOWLSubObjectPropertyOfAxiom(  (OWLObjectPropertyExpression)aprop1, (OWLObjectPropertyExpression)aprop2 );
				        	 //System.out.println( "sub p1 = " +  obUri1);
					         //System.out.println( "sub p2 = " +  obUri2);
				         } else {
				        	  
				        	 propAx = facto.getOWLSubObjectPropertyOfAxiom(  (OWLObjectPropertyExpression)prop2, (OWLObjectPropertyExpression)prop1 );
				        	 apropAx = facto.getOWLSubObjectPropertyOfAxiom(  (OWLObjectPropertyExpression)aprop2, (OWLObjectPropertyExpression)aprop1 );
	
				         }
				         AddAxiom addaxiom = new AddAxiom(initialAlignOnto, propAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						 
						 addaxiom = new AddAxiom(alignOnto, apropAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
			        	  
			         }
			         
			         if ( onto1.isDataProperty(ob1) && onto2.isDataProperty(ob2)) {
			        	 localMap.addRole(uri1, obUri1 );
			        	 localMap.addDataRole(uri1, obUri1 );
			        	 globalRoleMap.put(obUri1, aoUriStr + "#" + name1 );
			        	 globalDataRoleMap.put(obUri1, aoUriStr + "#" + name1 );
			        	 
			        	 localMap.addRole(uri2, obUri2 );
			        	 localMap.addDataRole(uri2, obUri2 );
			        	 globalRoleMap.put(obUri2, aoUriStr + "#" + name2 );
			        	 globalDataRoleMap.put(obUri2, aoUriStr + "#" + name2 );
			        	 
			        	 OWLProperty prop1 = facto.getOWLDataProperty( IRI.create( aoUri + "#" + name1 ) );
				         OWLProperty prop2 = facto.getOWLDataProperty( IRI.create( aoUri + "#" + name2 ) ); 
				         Set<OWLDataPropertyExpression> propSet = new HashSet<OWLDataPropertyExpression>(); 
				         propSet.add( (OWLDataPropertyExpression) prop1);
				         propSet.add( (OWLDataPropertyExpression) prop2);
				         OWLPropertyAxiom propAx = null;
				         
				         OWLProperty aprop1 = facto.getOWLDataProperty( IRI.create( obUri1 ) );
				         OWLProperty aprop2 = facto.getOWLDataProperty( IRI.create( obUri2 ) ); 
				         Set<OWLDataPropertyExpression> apropSet = new HashSet<OWLDataPropertyExpression>(); 
				         apropSet.add( (OWLDataPropertyExpression) aprop1);
				         apropSet.add( (OWLDataPropertyExpression) aprop2);
				         OWLPropertyAxiom apropAx = null;
				         if (cell.getRelation() instanceof EquivRelation) {
				        	 propAx = facto.getOWLEquivalentDataPropertiesAxiom( propSet );
				        	 apropAx = facto.getOWLEquivalentDataPropertiesAxiom( apropSet );
				        	 //System.out.println( "equi p1 = " +  obUri1);
					         //System.out.println( "equi p2 = " +  obUri2);
				        	  
				         } else if(cell.getRelation() instanceof SubsumedRelation ) {
				        	 propAx = facto.getOWLSubDataPropertyOfAxiom(  (OWLDataPropertyExpression)prop1, (OWLDataPropertyExpression)prop2 );
				        	 apropAx = facto.getOWLSubDataPropertyOfAxiom(  (OWLDataPropertyExpression)aprop1, (OWLDataPropertyExpression)aprop2 );
				        	 //System.out.println( "sub p1 = " +  obUri1);
					         //System.out.println( "sub p2 = " +  obUri2);
				         } else {
				        	  
				        	 propAx = facto.getOWLSubDataPropertyOfAxiom(  (OWLDataPropertyExpression)prop2, (OWLDataPropertyExpression)prop1 );
				        	 apropAx = facto.getOWLSubDataPropertyOfAxiom(  (OWLDataPropertyExpression)aprop2, (OWLDataPropertyExpression)aprop1 );
	
				         }
				         AddAxiom addaxiom = new AddAxiom(initialAlignOnto, propAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						 
						 addaxiom = new AddAxiom(alignOnto, apropAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
			        	  
			         }
				             
			         //there may be two kinds of individual correspondences : membership  
			          
			         if ( (onto1.isIndividual(ob1) && onto2.isClass(ob2))  ) {
			        	 localMap.addInd(uri1, obUri1 );
			        	  	 
			        	 globalIndMap.put(obUri1, aoUriStr + "#" + name1 );
			        	 
			        	 localMap.addConcept(uri2, obUri2 );
			        	 globalConceptMap.put(obUri2, aoUriStr + "#" + name2 );
			          
			        	 OWLIndividual ind1 = facto.getOWLNamedIndividual( IRI.create( aoUriStr + "#" + name1 ) );
			        	 OWLClass cls1 = facto.getOWLClass( IRI.create( aoUriStr + "#" + name2 ) );
				         OWLIndividualAxiom indAx = null;
				         
				         OWLIndividual aind1 = facto.getOWLNamedIndividual( IRI.create( obUri1 ) );
			        	 OWLClass acls1 = facto.getOWLClass( IRI.create( obUri2 ) );
				         OWLIndividualAxiom aindAx = null;
				         if (cell.getRelation() instanceof SubsumedRelation) {
				        	 
				        	 //System.out.println( "subsume d1 = " +  obUri1);
					         //System.out.println( "subsume c2 = " +  obUri2);
					         
					     indAx = facto.getOWLClassAssertionAxiom(cls1, ind1 );
					     aindAx = facto.getOWLClassAssertionAxiom(acls1, aind1 );
				         }
				         AddAxiom addaxiom = new AddAxiom(initialAlignOnto, indAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						 
						 addaxiom = new AddAxiom(alignOnto, aindAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						  
			         }
			         
			         if ( (onto1.isClass(ob1) && onto2.isIndividual(ob2))  ) {
			        	 localMap.addConcept(uri1, obUri1 );
			        	 globalConceptMap.put(obUri1, aoUriStr + "#" + name1 );
			        	 
			        	 localMap.addInd(uri2, obUri2 );
			        	 globalIndMap.put(obUri2, aoUriStr + "#" + name2 );
			         
			        	 OWLIndividual ind1 = facto.getOWLNamedIndividual( IRI.create( aoUriStr + "#" + name2 ) );
			        	 OWLClass cls1 = facto.getOWLClass( IRI.create( aoUriStr + "#" + name1 ) );
				         OWLIndividualAxiom indAx = null;
				         
				         OWLIndividual aind1 = facto.getOWLNamedIndividual( IRI.create( obUri2 ) );
			        	 OWLClass acls1 = facto.getOWLClass( IRI.create( obUri1 ) );
				         OWLIndividualAxiom aindAx = null;
				         if (cell.getRelation() instanceof SubsumeRelation) {
				        	 //indAx = facto.getOWLSameIndividualsAxiom( indSet );
					     indAx = facto.getOWLClassAssertionAxiom(cls1, ind1 );
					     aindAx = facto.getOWLClassAssertionAxiom(acls1, aind1 );
				        	 
				        	 //System.out.println( "subsumed c1 = " +  obUri1);
					         //System.out.println( "subsumed d2 = " +  obUri2);
				         }
				         
				         //System.out.println(" membership assertion added 2");
				         
				         AddAxiom addaxiom = new AddAxiom(initialAlignOnto, indAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
						 
						 addaxiom = new AddAxiom(alignOnto, aindAx);
						 //addaxiom.accept(xc);
						 manager.applyChange( addaxiom );
			         }
			          
			     }
			    
			} catch (Exception ex) { ex.printStackTrace(); } 
			
			 
		}
	}

	public HashMap<String, String> getGlobalConceptMap() {
	       return globalConceptMap;
	
	}
	
	
	public HashMap<String, String> getGlobalObjectRoleMap() {
	       return globalDataRoleMap;
	
	}
	
	public HashMap<String, String> getGlobalDataRoleMap() {
	       return globalDataRoleMap;
	
	}
	
	public HashMap<String, String> getGlobalRoleMap() {
	       return globalDataRoleMap;
	
	}
	
	public LocalEntityMap getLocalMap() {
	       return localMap;
	
	}
	
	public OWLOntology getOWLAlignmentOntology() {
	       return initialAlignOnto;
	}
	
	public OWLOntology getOWLAlignOnto() {
	       return alignOnto;
	}
	
	public String getLCsByGC(String g) {
		Set<String> gl = globalConceptMap.keySet();
		//Set<String> res =  new HashSet();
		for( String t :  gl) {
			if(globalConceptMap.get(t).equals(g)) return t;
		}
		return null;
	}
	
	public Set<String> getLCsByGC(Set<String> g) {
		Set<String> res =  new HashSet();
		for( String t :  g) {
			res.add( getLCsByGC(t) );
		}
		return res;
	}
	
	public String getLRsByGR(String g) {
		Set<String> gl = globalRoleMap.keySet();
		//Set<String> res =  new HashSet();
		for( String t :  gl) {
			if(globalRoleMap.get(t).equals(g)) return t; 
		}
		return null;		
	}
	
	public Set<String> getLRsByGR(Set<String> g) {
		Set<String> res =  new HashSet();
		for( String t :  g) {
			res.add( getLRsByGR(t) );
		}
		return res;
	}
	
	public Set<String> getLocalUris() {
		return localUris;
	}

	public Set<String> getAlignLocations() {
		return alignLocations;
	}
}
